const xlsxReader = require("node-xlsx");
const { writeFile: _writeFile } = require('fs');



const writeFile = (outputFileName, jsonData) => {
    // 文件编码格式  utf-8
    _writeFile(outputFileName, jsonData, 'utf-8', (err) => {
      if (!err) {
        console.log('文件生成成功'); 
      } else {
        console.log('文件生成失败');
      }
    });
};



const readFile = async () => {
    var fileData = xlsxReader.parse("./excelFiles/excel.xlsx");

    for (let i = 0; i < fileData.length; i++){
        // console.log(fileData[i].data);
        const objKeys = fileData[i].data.slice(0, 1).flat();
        const objValues = fileData[i].data.slice(1, fileData[i].data.length);
        console.log("objKeys: ", objKeys);
        console.log("objValues: ", objValues);
        var resultList = [];
        for (let j = 0; j < objValues.length; j++){
            let obj = {};
            for (let k = 0; k < objValues[j].length; k++){
                obj[objKeys[k]] = objValues[j][k]
            }
            resultList.push(obj);
        }
        await writeFile(`./jsonFiles/${fileData[i].name}.json`, JSON.stringify(resultList));
    }
}

readFile();